package com.adinnet.api.user.controller;


import com.adinnet.api.common.model.AuthUtil;
import com.adinnet.api.user.model.User;
import com.adinnet.api.user.model.vo.LoginReq;
import com.adinnet.api.user.model.vo.UpdatePwdReq;
import com.adinnet.api.user.model.vo.VerificationCodeReq;
import com.adinnet.api.user.model.vo.VerifyCodeReq;
import com.adinnet.api.user.service.UserService;
import com.adinnet.common.utils.ResponseUtil;
import com.adinnet.core.BizResult;
import com.adinnet.core.JsonResult;
import com.adinnet.core.exception.BizException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-02
 */
@RestController
@RequestMapping("/api/user")
public class UserController {


    @Autowired
    private UserService userService;

    /**
     * 获取验证码
     *
     * @param verifyCodeReq
     * @return
     */
    @PostMapping("sendCode")
    public JsonResult sendCode(@Valid @RequestBody VerifyCodeReq verifyCodeReq) {
        try {

            boolean sendCode = userService.sendCode(verifyCodeReq.getMobile(), verifyCodeReq.getType());
            if (sendCode) {
                return JsonResult.ok("发送成功!");
            }
            return JsonResult.error("发送失败!");
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("获取验证码失败");
        }
    }

    /**
     * 手机密码登录
     *
     * @param loginReq
     * @param request
     * @return
     */
    @PostMapping("/login")
    public JsonResult login(@Valid @RequestBody LoginReq loginReq, HttpServletRequest request) {
        try {

            return userService.login(loginReq.getMobile(), loginReq.getPwd(), request,loginReq.getReceiveUserId());
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("手机密码登录失败");
        }
    }

    /**
     * 修改密码/忘记密码
     *
     * @param updatePwdReq
     * @param request
     * @return
     */
    @PostMapping("update/pwd")
    public JsonResult updatePwd(@Valid @RequestBody UpdatePwdReq updatePwdReq, HttpServletRequest request) {
        try {
            return userService.updatePwd(updatePwdReq.getMobile(), updatePwdReq.getPwd(), updatePwdReq.getCode(), request);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("修改密码失败");
        }
    }

    /**
     * 注销
     *
     * @return
     */
    @PostMapping("/logoff")
    public JsonResult logoff(HttpServletRequest request, HttpServletResponse response) {
        try {
            User loginUser = AuthUtil.getLoginUser();
            if (null == loginUser) {
                ResponseUtil.responeJsonStr(200, response, new BizResult<>(401, "登陆过期或失效，请重新登录"));
                return JsonResult.error();
            }
            return userService.logoff(loginUser.getTelNo(), request);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("注销失败");
        }
    }

    /**
     * 退出登录
     *
     * @return
     */
    @PostMapping("/logout")
    public JsonResult logout(HttpServletRequest request, HttpServletResponse response) {
        try {
            User loginUser = AuthUtil.getLoginUser();
            if (null == loginUser) {
                ResponseUtil.responeJsonStr(200, response, new BizResult<>(401, "登陆过期或失效，请重新登录"));
                return JsonResult.error();
            }
            return userService.logout(loginUser.getTelNo(),request,response);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("退出失败！");
        }
    }

    /**
     * 校验验证码
     *
     * @param verificationCodeReq
     * @param request
     * @return
     */
    @PostMapping("check/verification")
    public JsonResult checkVerification(@Valid @RequestBody VerificationCodeReq verificationCodeReq, HttpServletRequest request) {
        try {
            return userService.checkVerification(verificationCodeReq.getMobile(), verificationCodeReq.getCode(), request);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("校验验证码失败");
        }
    }

    /**
     * 校验token
     *
     * @param token
     * @return
     */
    @GetMapping("check/token")
    public JsonResult checkToken(String token, HttpServletRequest request) {
        try {
            if (StringUtils.isEmpty(token)) {
                return JsonResult.error("token不能为空!");
            }
            return userService.checkToken(token, request);
        } catch (BizException e) {
            return JsonResult.error(e.getMessage());
        } catch (Exception e) {
            return JsonResult.error("校验Token失败");
        }
    }
}
